package ru.ifmo.filemanager.filesystem;

import java.io.IOException;

import ru.ifmo.filemanager.filesystem.interfaces.Directory;
import ru.ifmo.filemanager.filesystem.interfaces.DirectoryLookupReceiver;
import ru.ifmo.filemanager.filesystem.interfaces.FileSystemObject;

public class DirectoryLookupNotifier implements DirectoryLookupReceiver {

	private boolean completed;
	private FileSystemObject result;
	private IOException error;

	public synchronized void directoryLookupSuccess(Directory self, String name,
		FileSystemObject result) {
		completed = true;
		this.result = result;
		notifyAll();
	}
	
	public synchronized void directoryLookupFailure(Directory self, String name,
		IOException error) {
		completed = true;
		this.error = error;
		notifyAll();
	}

	public synchronized FileSystemObject await() throws IOException {
		while (!completed) {
			try {
				wait();
			} catch (InterruptedException e) {
				throw new InterruptedAsyncIOException(e);
			}
		}
		if (error != null) {
			throw error;
		} else {
			return result;
		}
	}
}